package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PartnerRadioAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceBus;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Classname PartnerRadioAttrHandler
 * @Description 合作伙伴为新增时，默认带出，格式为：运营方-客户名称-行业类型，可修改，需调BOSS平台的合作伙伴新增查询接口进行校验，校验通过则允许保存，校验不通过则不允许保存，提示：‘合作伙伴已存在并无法保存，请修改’；
 * 合作伙伴为复用时，需调BOSS平台的合作伙伴复用查询接口获取合作伙伴列表下拉框选择；
 * 运营方专员调BOSS平台的获取运营方下运营专员接口获取下拉框选择；
 * @Author yang.guobin
 * @Date 2021-04-27 20:50
 */
@Service
@AttrHandler(PartnerRadioAttrHandler.class)
public class PartnerRadioAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    OperateInstAttrService operateInstAttrService;
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private InfServiceBus infServiceBus;
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String group_user_name="";
        String opp_user_name="";
        List<KiteAttr> groupUserkiteAttrs = querySceneInstService
            .listCommonAttrsByModuleId(data.getSceneInstId(), LocalKeyConsts.GROUPUSERINFO_MODULEID).stream().filter(
            //.listCommonAttrsByModuleId(KiteWebContext.getContextNew().getCurrentSceneInstId(), LocalKeyConsts.GROUPUSERINFO_MODULEID).stream().filter(
                kiteAttr -> !KiteStringUtils.isEqual("H", kiteAttr.getShowType()) && !KiteStringUtils
                    .isEqual("1", kiteAttr.getObjId())).collect(Collectors.toList());
        for (KiteAttr kiteAttr : groupUserkiteAttrs) {
            if (KiteStringUtils.equals("group_user_name", kiteAttr.getFieldName())) {
                group_user_name = kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }else  if(KiteStringUtils.equals("USR0001", kiteAttr.getFieldName())){
                opp_user_name=kiteAttr.getValue() == null ? "" : kiteAttr.getValueDesc();
            }
        }

        // 合作伙伴类型
        KiteAttr attr1 = inst.getAttr("200004284");
        // 合作伙伴id
        KiteAttr attr3 = inst.getAttr("200001192");
        //合作伙伴名称
        KiteAttr attr2 = inst.getAttr("200001193");
        //运营方
        KiteAttr attr4 = inst.getAttr("200001046");
        //业务行业类型
        KiteAttr attr5 = inst.getAttr("200001047");

        String nodeCode = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "nodeCode", "");

        //重提初始化不校验
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode")) {
            return;
        }
        // 变更业务的场景初始化过程中不校验
        if (!"1068".equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            return;
        }
        if(KiteStringUtils.equals("100006370", attr1.getValue())) {//新增

            attr2.setAttrValueTypeId("98C");
            Map map=new HashMap();
            if(KiteStringUtils.isNotEmpty(group_user_name)&&!"OrderResubmit".equals(nodeCode)){
            map.put("ENT_NAME",attr4.getValueDesc()+"-"+group_user_name+"-"+attr5.getValueDesc());
            Map<String, Object> detailMap = infServiceBus.queryNewPartnerQuery(map);
            String RESULT_CODE= (String) detailMap.get("RESULT_CODE");
            if(RESULT_CODE.equals("0005")){
               ErrorConsts.INF_INVOKE_ERROR.throwOut(LocalCallClientEnum.INF_COMMON_URL.getServiceCode(), "合作伙伴已存在并无法保存，请修改", "");
                attr2.setValue("");
                attr2.setValueDesc("");
            }else{
                attr2.setValue(attr4.getValueDesc()+"-"+group_user_name+"-"+attr5.getValueDesc());
                attr2.setValueDesc(attr4.getValueDesc()+"-"+group_user_name+"-"+attr5.getValueDesc());
            }
            }
        }else  if(KiteStringUtils.equals("100007576", attr1.getValue())) {//复用
            String oppoNum=this.querySceneInstService.getExtParameterValue(data.getSceneInstId(), "oppoNum");
            if(KiteStringUtils.isEmpty(oppoNum)){
                oppoNum=opp_user_name;
            }
            Map map=new HashMap();
            map.put("BUSINESS_NO",oppoNum);
            Map<String, Object> datamap = infServiceBus.partnerReuseQuery(map);
            List<String> list = (List) datamap.get("RESULT_DATA");
            List<KiteAttrValue> attrValues = new ArrayList<>();
            list.stream().forEach(name -> {
                KiteAttrValue attrValue = new KiteAttrValue();
                attrValue.setAttrValue(name);
                attrValue.setAttrValueName(name);
                attrValues.add(attrValue);
            });
            attr2.setAttrValueTypeId("98A");
            attr2.setAttrValues(attrValues);
        }

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
